package com.sys.listener;

import com.sys.entity.SysMenu;
import com.sys.interceptor.SysInterceptor;
import com.sys.service.SysCrontabService;
import com.sys.service.SysMenuService;
import com.sys.utils.Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @Author bhudyA
 * @Description 监听器
 * @Date 2019-10-30 23:09:37
 */
@WebListener
public class SysListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("FirstListener:Servlet容器初始化...");

        ServletContext ctx = sce.getServletContext();
        Utils.init(WebApplicationContextUtils.getWebApplicationContext(ctx));

        SysMenuService sysMenuService = Utils.getBean(SysMenuService.class);
        List<SysMenu> sysMenuList = sysMenuService.findAll();
        if (Utils.isBlank(sysMenuList)) {
            for (SysMenu sysMenu : sysMenuList) {
                if (Utils.isBlank(sysMenu.getUrl())) {
                    SysInterceptor.idSysMenuMap.put(sysMenu.getUrl(), sysMenu);
                }
            }
        }


    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("FirstListener:Servlet容器被销毁了");
    }

}